package com.example.commuity.viewmodel

import android.util.Log
import androidx.lifecycle.viewModelScope
import com.example.common.viewmodel.BaseViewModel
import com.example.commuity.entity.AutoPlayFollowCardEntity
import com.example.commuity.entity.CommunityColumnsCardEntity
import com.example.commuity.entity.viewdata.AttentionFollowViewData
import com.example.commuity.entity.viewdata.RecommendFollowViewData
import com.example.commuity.model.CommunityRepository
import com.example.commuity.uistate.CommunityUiState
import com.example.entity.BaseEntity
import com.example.entity.NetDataEntity
import com.example.network.ApiUrl
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import javax.inject.Inject
/**
 * 社区的VIewModel层
 * **/

@HiltViewModel
class CommunityViewModel @Inject constructor(private val repository: CommunityRepository): BaseViewModel<CommunityUiState>() {

    private var nextCommendPageUrl = ApiUrl.COMMUNITY_RECOMMEND
    private var nextAttentionPageUrl = ApiUrl.COMMUNITY_ATTENTION

    /**
     * 推荐列表获取数据
     * */
    fun loadRecommendData(isLoadMore :Boolean =false){
        viewModelScope.launch (Dispatchers.IO){
            try {
                //获取接口数据
                var result = if (!isLoadMore) repository.getRecommendList() else repository.getRecommendList(nextCommendPageUrl)
                //更新下一页加载数据
                var nexPageParms = result.nextPageUrl.substring(result.nextPageUrl.indexOf("?")+1)
                nextCommendPageUrl="${ApiUrl.COMMUNITY_RECOMMEND}?${nexPageParms}"
                Log.i("TAG", "下一页请求数据URL地址: $nextCommendPageUrl")

                //判断返回数据
                var itemList = result.itemMap as MutableList<NetDataEntity>

                //数据裁剪
                var parseResultList : List<BaseEntity> = parseRecommendViewData(itemList)

                if (parseResultList.isNotEmpty()){
                    unPeekLiveData.postValue(CommunityUiState.loadRecommendSuccess(parseResultList))
                }else{
                    unPeekLiveData.postValue(CommunityUiState.loadRecommendFail("数据为空"))
                }

            }catch (e:Exception){
                e.printStackTrace()
                this.cancel()
            }
        }
    }

    /**
     * 关注列表获取数据
     * */
    fun loadAttentionData(isLoadMore :Boolean =false){
        viewModelScope.launch (Dispatchers.IO){
            try {
                //获取接口数据
                var result = if (!isLoadMore) repository.getAttentionList() else repository.getAttentionList(nextAttentionPageUrl)
                //更新下一页加载数据
                var nexPageParms = result.nextPageUrl.substring(result.nextPageUrl.indexOf("?")+1)
                nextAttentionPageUrl="${ApiUrl.COMMUNITY_ATTENTION}?${nexPageParms}"

                //判断返回数据
                var itemList = result.itemMap as MutableList<NetDataEntity>

                //数据裁剪
                var parseResultList : List<BaseEntity> = parseAttentionViewData(itemList)

                if (parseResultList.isNotEmpty()){
                    unPeekLiveData.postValue(CommunityUiState.loadAttentionSuccess(parseResultList))
                }else{
                    unPeekLiveData.postValue(CommunityUiState.loadAttentionFail("数据为空"))
                }

            }catch (e:Exception){
                e.printStackTrace()
                this.cancel()
            }
        }
    }

    /**
     * 关注
     * */
    private fun parseAttentionViewData(itemList: MutableList<NetDataEntity>): List<BaseEntity> {
        var resultList = mutableListOf<BaseEntity>()
        itemList.forEach {
            when(it.type){
                "autoPlayFollowCard" ->parseAttentionCardViewData(resultList,it.value)
            }
        }
        return resultList
    }

    private fun parseAttentionCardViewData(list: MutableList<BaseEntity>, value: BaseEntity) {
        var entity = value as AutoPlayFollowCardEntity
        list.add(AttentionFollowViewData(
            entity.data.header.icon,
            entity.data.header.issuerName,
            entity.data.header.time,
            entity.data.content.data.title,
            entity.data.content.data.description,
            entity.data.content.data.cover.feed,
            entity.data.content.data.cover.blurred,
            entity.data.content.data.playUrl,
            entity.data.content.data.category,
            entity.data.content.data.author.description,
            entity.data.content.data.id,
            entity.data.content.data.consumption.collectionCount,
            entity.data.content.data.consumption.shareCount,
            entity.data.content.data.consumption.replyCount,
            entity.data.content.data.consumption.realCollectionCount
        ))
    }

    /**
     * 推荐
     * */
    private fun parseRecommendViewData(itemList: MutableList<NetDataEntity>): List<BaseEntity> {
        var resultList = mutableListOf<BaseEntity>()
        itemList.forEach {
            when(it.type){
                "horizontalScrollCard"->resultList.add(it.value)
                "communityColumnsCard"->parseRecommendFollowCard(resultList,it.value)
            }
        }
        return resultList
    }

    private fun parseRecommendFollowCard(resultList: MutableList<BaseEntity>, value: BaseEntity) {
        var entity = value as CommunityColumnsCardEntity
        resultList.add(RecommendFollowViewData(
            entity.data.content.data.cover.feed,
            entity.data.content.data.description,
            entity.data.content.data.owner.avatar,
            entity.data.content.data.owner.nickname,
            entity.data.content.data.consumption.collectionCount,
            entity.data.content.data.width,
            entity.data.content.data.height
        ))
    }

}